package servlet;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.UserDao;
import model.User;

public class MemberJoinAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		User user = new User();
		UserDao userDao = new UserDao();

		ActionForward forward = null;

		String memid = request.getParameter("memid") != null
				&& request.getParameter("memid") != "" ? request
				.getParameter("memid") : null;
		String passwd = request.getParameter("passwd") != null
				&& request.getParameter("passwd") != "" ? request
				.getParameter("passwd") : null;
		String name = request.getParameter("name") != null
				&& request.getParameter("name") != "" ? request
				.getParameter("name") : null;
		String email = request.getParameter("email") != null
				&& request.getParameter("email") != "" ? request
				.getParameter("email") : null;
		String pNum = request.getParameter("pNum") != null
				&& request.getParameter("pNum") != "" ? request
				.getParameter("pNum") : null;
		String univ = request.getParameter("univ") != null
				&& request.getParameter("univ") != "" ? request
				.getParameter("univ") : null;
		String grade = request.getParameter("grade") != null
				&& request.getParameter("grade") != "" ? request
				.getParameter("grade") : null;
		int langid1 = request.getParameter("langid1") != null
				&& request.getParameter("langid1") != "" ? Integer
				.parseInt(request.getParameter("langid1")) : 0;
		int langid2 = request.getParameter("langid2") != null
				&& request.getParameter("langid2") != "" ? Integer
				.parseInt(request.getParameter("langid2")) : 0;
		int langid3 = request.getParameter("langid3") != null
				&& request.getParameter("langid3") != "" ? Integer
				.parseInt(request.getParameter("langid3")) : 0;
		int langGrade1 = request.getParameter("langGrade1") != null
				&& request.getParameter("langGrade1") != "" ? Integer
				.parseInt(request.getParameter("langGrade1")) : 0;
		int langGrade2 = request.getParameter("langGrade2") != null
				&& request.getParameter("langGrade2") != "" ? Integer
				.parseInt(request.getParameter("langGrade2")) : 0;
		int langGrade3 = request.getParameter("langGrade3") != null
				&& request.getParameter("langGrade3") != "" ? Integer
				.parseInt(request.getParameter("langGrade3")) : 0;

		if (memid == null || passwd == null || name == null || email == null || pNum == null) {

			Util.ShowError(response, "회원가입이 실패했습니다.");

			return null;

		}

		name=Util.toUTF8(name);
		univ=Util.toUTF8(univ);
		
		user.setMemid(memid);
		user.setPasswd(passwd);
		user.setName(name);
		user.setEmail(email);
		user.setpNum(pNum);
		user.setUniv(univ);
		user.setGrade(grade);
		user.setLangid1(langid1);
		user.setLangGrade1(langGrade1);
		user.setLangid2(langid2);
		user.setLangGrade2(langGrade2);
		user.setLangid3(langid3);
		user.setLangGrade3(langGrade3);

		boolean isSuccess=userDao.insertMember(user);

		if(isSuccess)
		{
			Util.ShowSuccess(response, "회원가입이 완료되었습니다.", "./questudy.jsp");
		}
		else
		{
			Util.ShowError(response, "회원가입에 실패하였습니다.");
		}
		
		

		return forward;
	}

}
